/**
 * BMFontLoader.java
 *
 * Roslan's Remake of MOO in 3D using Ardor3D
 * (C) 2010-2012 Roslan Amir. All rights reserved.
 */

package name.roslan.games.moo3d.utils;

import java.util.ArrayList;
import java.util.List;

import name.roslan.games.moo3d.GameBase;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ardor3d.ui.text.BMFont;
import com.ardor3d.util.resource.ResourceLocatorTool;
import com.ardor3d.util.resource.ResourceSource;

/**
 * Singleton BMFont loader class that loads some font textures and provides easy access.
 * 
 * @author Roslan Amir
 * @version 1.0 - Aug 6, 2011
 */
public class BMFontLoader {

    /**
     * Logger object to handle all logging activities.
     */
    private static final Logger logger = LoggerFactory.getLogger(GameBase.class);

    /**
	 * 
	 */
    private static BMFontLoader _instance;

    /**
	 * 
	 */
    private final ArrayList<BMFont> _fontList = new ArrayList<BMFont>();

    /**
     * Comments
     * 
     * @return
     */
    private static BMFontLoader instance() {
        if (_instance == null) {
            _instance = new BMFontLoader();
        }
        return _instance;
    }

    /**
     * Comments
     * 
     * @return
     */
    public static List<BMFont> getAllFonts() {
        return instance()._fontList;
    }

    /**
     * Comments
     * 
     * @return
     */
    public static BMFont getDefaultFont() {
        return instance()._fontList.get(0);
    }

    /**
     * Comments
     * 
     * @param i
     * @return
     */
    public static BMFont getFontAt(int i) {
        if (i < 0 || i >= instance()._fontList.size())
            i = 0;
        return instance()._fontList.get(i);
    }

    /**
     * Private constructor.
     */
    private BMFontLoader() {
        final FontLoad[] fontNames = new FontLoad[] {
                new FontLoad("CenturyGothicBold", true),
                new FontLoad("Computerfont", true),
                new FontLoad("ComputerfontBold", true) };

        for (final FontLoad fl : fontNames) {
            try {
                final String file = "fonts/" + fl.fontName + ".fnt";
                final ResourceSource url = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, file);
                final BMFont bmFont = new BMFont(url, fl.useMipMaps);
                _fontList.add(bmFont);
            } catch (final Throwable t) {
                logger.warn(t.getMessage());
            }
        }
        logger.info("defaultFont = " + _fontList.get(0).getStyleName());
    }

    /**
     * Comments...
     * 
     * @author Roslan Amir
     * @version 1.0 - Aug 6, 2011
     */
    private static class FontLoad {

        String fontName;

        boolean useMipMaps;

        FontLoad(final String name, final boolean mipMap) {
            fontName = name;
            useMipMaps = mipMap;
        }
    }

}
